package cn.lf.service.impl;

import cn.lf.dto.index.IndexHeadInfoRepDTO;
import cn.lf.dto.index.IndexHeadInfoReqDTO;
import cn.lf.entity.OrderInfo;
import cn.lf.entity.Product;
import cn.lf.entity.SysUser;
import cn.lf.mapper.OrderInfoMapper;
import cn.lf.mapper.ProductMapper;
import cn.lf.mapper.SysUserMapper;
import cn.lf.service.IndexService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author guoyy
 */
@Service
public class IndexServiceImpl implements IndexService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private ProductMapper productMapper;

    @Resource
    private OrderInfoMapper orderInfoMapper;


    @Override
    public IndexHeadInfoRepDTO getHeadInfo(IndexHeadInfoReqDTO indexHeadInfoReqDTO) {

        IndexHeadInfoRepDTO result= new IndexHeadInfoRepDTO();

        result.setUserNumber(getUserNumber());
        result.setProductNumber(getProductNumber(indexHeadInfoReqDTO.getShopId()));
        result.setOrderNumber(getOrderNumber(indexHeadInfoReqDTO.getShopId()));

        return result;
    }

    /**
     * 获取用户数量
     * @return
     */
    private Integer getUserNumber(){

        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();

        List<SysUser> sysUserList = sysUserMapper.selectList(wrapper);

        return sysUserList.size();
    }

    /**
     * 获取商品数量
     * @return
     */
    private Integer getProductNumber(String shopId){

        return productMapper.countIndexPn(shopId);
    }

    /**
     * 获取订单数量
     * @return
     */
    private Integer getOrderNumber(String shopId){
        LambdaQueryWrapper<OrderInfo> wrapper = new LambdaQueryWrapper<>();

        if (Objects.nonNull(shopId)&&!Objects.equals(shopId,"")){
            wrapper.eq(OrderInfo::getShopId,shopId);
        }

        List<OrderInfo> orderInfoList = orderInfoMapper.selectList(wrapper);

        return orderInfoList.size();
    }
}
